/*
 * @Description: 全宗reducer
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-06-01 18:53:38
 */

import { delItemByIdArray, insertItem } from '@/utils/list';
import {
  ADD_AT_TYPE,
  ARC_TYPE_LOADING,
  DELETE_AT_TYPE,
  SET_ARC_TYPE_LIST,
  SET_SELECT_FOND,
  UPDATE_AT_REPO
} from '../constants';

export const ArcTypeReducer = (state, action) => {
  switch (action.type) {
    // 表格加载loading
    case ARC_TYPE_LOADING:
      return { ...state, loading: true };

    /**
     * @description: 加载表格数据及相关数据
     * @param {Array} data 表格数据
     * @return {*}
     */
    case SET_ARC_TYPE_LIST: {
      const ATList = action.data.map((item) => {
        item.treeName = item.atName;
        item.key = item.id;
        return item;
      });
      return {
        ...state,
        ATList
      };
    }

    /**
     * @description: 设置选中全宗
     * @param {Object} data 选中的全宗
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 添加档案类型
     * @param {Object} data 新增的档案类型
     */
    case ADD_AT_TYPE:
      return {
        ...state,
        ATList: insertItem(action.data, state.ATList)
      };

    /**
     * @description: 删除档案类型
     * @param {number[]} ids 删除档案类型的ID
     */
    case DELETE_AT_TYPE:
      return {
        ...state,
        ATList: delItemByIdArray(action.ids, state.ATList)
      };

    /**
     * @description: 库及层级相关信息
     * @param {number[]} data 更新的信息
     */
    case UPDATE_AT_REPO:
      return {
        ...state,
        repoInfo: action.data
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
